using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Common;
using Common.NotifyEvent;

namespace SExperiment.ExperimentResult
{
	[Serializable]
	public class BaseResult : NotNotifyPropertyChanged
	{
		[NonSerialized]
		private ObservableCollection<Conclusion> _ConclusionList = new ObservableCollection<Conclusion>();

		[NonSerialized]
		private List<MidValue> _MidValues = new List<MidValue>();

		public ObservableCollection<Conclusion> ConclusionList
		{
			get
			{
				return _ConclusionList;
			}
		}

		public List<MidValue> MidValues
		{
			get
			{
				return _MidValues;
			}
		}

		[OnDeserialized]
		private void OnDeserialized(StreamingContext context)
		{
			if (_ConclusionList == null)
			{
				_ConclusionList = new ObservableCollection<Conclusion>();
			}
			if (_MidValues == null)
			{
				_MidValues = new List<MidValue>();
			}
		}

		public string GetAllConclusion()
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (Conclusion conclusion in _ConclusionList)
			{
				stringBuilder.Append(conclusion.Content);
			}
			return stringBuilder.ToString();
		}

		public void SaveToStream(StreamEx stream)
		{
			stream.Write(PropertyKey.Key_BaseResult);
			List<PropertyKey> keys = new List<PropertyKey>();
			List<byte[]> list = new List<byte[]>();
			GetDatesAndKeys(keys, list);
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(keys, list);
			stream.Flush();
		}

		public virtual void GetDatesAndKeys(List<PropertyKey> keys, List<byte[]> datas)
		{
			StreamEx streamEx = new StreamEx();
			streamEx.Write(ConclusionList.Count);
			foreach (Conclusion conclusion in ConclusionList)
			{
				conclusion.SaveToStream(streamEx);
			}
			keys.Add(PropertyKey.Key_Items);
			datas.Add(streamEx.ToArray());
			streamEx.Close();
			_MidValues = MidValues.Where((MidValue s) => !string.IsNullOrEmpty(s.Name) && !string.IsNullOrEmpty(s.Value)).ToList();
			if (MidValues.Count <= 0)
			{
				return;
			}
			streamEx = new StreamEx();
			streamEx.Write(MidValues.Count);
			foreach (MidValue midValue in MidValues)
			{
				streamEx.Write(midValue.Name);
				streamEx.Write(midValue.Value);
			}
			keys.Add(PropertyKey.Key_HashtableMidValue);
			datas.Add(streamEx.ToArray());
			streamEx.Close();
		}

		public virtual void SetDatesAndKeys(List<PropertyKey> keys, List<byte[]> datas)
		{
			for (int i = 0; i < keys.Count; i++)
			{
				switch (keys[i])
				{
				case PropertyKey.Key_Items:
				{
					StreamEx streamEx = new StreamEx();
					streamEx.Write(datas[i]);
					streamEx.Seek(0L, SeekOrigin.Begin);
					int num = streamEx.ReadInt();
					for (int k = 0; k < num; k++)
					{
						Conclusion conclusion = new Conclusion();
						conclusion.ReadFromStream(streamEx);
						ConclusionList.Add(conclusion);
					}
					streamEx.Close();
					break;
				}
				case PropertyKey.Key_HashtableMidValue:
				{
					StreamEx streamEx = new StreamEx();
					streamEx.Write(datas[i]);
					streamEx.Seek(0L, SeekOrigin.Begin);
					int num = streamEx.ReadInt();
					MidValues.Clear();
					for (int j = 0; j < num; j++)
					{
						MidValues.Add(new MidValue
						{
							Name = streamEx.ReadString(),
							Value = streamEx.ReadString()
						});
					}
					streamEx.Close();
					break;
				}
				}
			}
		}

		public bool ReadFromStream(StreamEx stream)
		{
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_BaseResult)
			{
				propertyKey = stream.ReadKey();
				if (propertyKey == PropertyKey.Key_Index)
				{
					List<PropertyKey> keys;
					List<byte[]> dates;
					stream.ReadIndex(out keys, out dates);
					SetDatesAndKeys(keys, dates);
					return true;
				}
				stream.Seek(-4L, SeekOrigin.Current);
				return false;
			}
			stream.Seek(-4L, SeekOrigin.Current);
			return false;
		}
	}
}
